﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBAccessLib;
using System.IO;

namespace LineWatch_WinForm
{
    public partial class PositionManagementView : Form
    {
        public bool isClosedApp = false;
        private string shiftName = string.Empty;
        private bool hasSHAPositionLine1 = false;
        private bool hasHPositionLine1 = false;
        private bool hasSAPositionLine1 = false;

        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine1 = new Dictionary<int, PositionStatusInfo>();

        public PositionManagementView()
        {
            InitializeComponent();
            this.timer.Enabled = false;

            this.InitFont();

            this.ClearPositionInfo();

#if DEBUG
            this.TopMost = false;
#else
            this.TopMost = true;
#endif
        }

        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PositionManagementView_Load(object sender, EventArgs e)
        {
            this.InitOperationArea();
            this.timer.Enabled = true;
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            if (ConfigInfo.CurrentLineCodeCount <= 0)
            {
                this.tlpMain.Visible = false;
            }

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                this.shiftName = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys1);
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        private void InitFont()
        {
            foreach (Control control in this.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        /// <param name="controlParent"></param>
        private void InitFont(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Update Font
        /// </summary>
        /// <param name="control"></param>
        private void UpdateFont(Control control)
        {
            if (control.Tag.ToString().StartsWith("FontSizeValue1"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.PositionFontSizeValue1, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue2"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.PositionFontSizeValue2, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("CommentFontSizeCaption"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.CommentFontSizeCaption, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("CommentFontSizeValue"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.CommentFontSizeValue, FontStyle.Bold);
            }
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Clear Position Info
        /// </summary>
        private void ClearPositionInfo()
        {
            this.hasSHAPositionLine1 = false;
            this.hasHPositionLine1 = false;
            this.hasSAPositionLine1 = false;

            this.lblArea1PositionInfo1.Text = string.Empty;
            this.lblArea1PositionInfo2.Text = string.Empty;
            this.lblArea1PositionInfo3.Text = string.Empty;
            this.lblArea1PositionInfo4.Text = string.Empty;
            this.lblArea1PositionInfo5.Text = string.Empty;
            this.lblArea1PositionInfo6.Text = string.Empty;
            this.lblArea1PositionInfo7.Text = string.Empty;
            this.lblArea1PositionInfo8.Text = string.Empty;
            this.lblArea1PositionInfo9.Text = string.Empty;
            this.lblArea1PositionInfo10.Text = string.Empty;

            this.lblArea2PositionInfo1.Text = string.Empty;
            this.lblArea2PositionInfo2.Text = string.Empty;
            this.lblArea2PositionInfo3.Text = string.Empty;
        }

        /// <summary>
        /// Get Position Data Row
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        private DataRow GetPositionDataRow(int position, string lineCode)
        {
            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                    {
                        if ((position == dmPosition)
                            && string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                        {
                            return row;
                        }
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Get Position Type
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private PositionType GetPositionType(DataRow row)
        {
            if (row != null)
            {
                if (string.Equals("1", row["Content2"].ToString().Trim()))
                {
                    return PositionType.Help;
                }
                else if (string.Equals("1", row["Content3"].ToString().Trim()))
                {
                    return PositionType.Alarm;
                }
                else if (string.Equals("1", row["Content4"].ToString().Trim()))
                {
                    return PositionType.Stop;
                }
                else
                {
                    return PositionType.Unknown;
                }
            }

            return PositionType.Null;
        }

        /// <summary>
        /// Get Position Desc
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetPositionDesc(DataRow row)
        {
            if (row != null)
            {
                ////string fPositionID = row["FPositionID"].ToString().Trim();

                string positionName = row["Position"].ToString().Trim();

                return positionName;
            }

            return string.Empty;
        }

        /// <summary>
        /// Get SPositionID
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetSPositionID(DataRow row)
        {
            if (row != null)
            {
                string sPositionID = row["SPositionID"].ToString().Trim();

                return sPositionID;
            }

            return string.Empty;
        }

        /// <summary>
        /// Get FPositionID
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetFPositionID(DataRow row)
        {
            if (row != null)
            {
                string fPositionID = row["FPositionID"].ToString().Trim();

                return fPositionID;
            }

            return string.Empty;
        }

        /// <summary>
        /// Set Operation Area
        /// </summary>
        /// <param name="positionCount"></param>
        private void SetOperationArea(int positionCount)
        {
            if (positionCount > 3)
            {
                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = false;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
            }
            else
            {
                this.tlpOperationArea1.Visible = false;
                this.tlpOperationArea2.Visible = true;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
            }
        }

        /// <summary>
        /// Update Help Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateHelpPositionInfo(string positionDesc, int positionCount)
        {
            this.hasSHAPositionLine1 = true;
            this.hasHPositionLine1 = true;

            if (positionCount > 3)
            {
                if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo1.Text))
                {
                    this.lblArea1PositionInfo1.Text = positionDesc;
                    this.lblArea1PositionInfo1.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo2.Text))
                {
                    this.lblArea1PositionInfo2.Text = positionDesc;
                    this.lblArea1PositionInfo2.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo3.Text))
                {
                    this.lblArea1PositionInfo3.Text = positionDesc;
                    this.lblArea1PositionInfo3.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo4.Text))
                {
                    this.lblArea1PositionInfo4.Text = positionDesc;
                    this.lblArea1PositionInfo4.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo5.Text))
                {
                    this.lblArea1PositionInfo5.Text = positionDesc;
                    this.lblArea1PositionInfo5.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo6.Text))
                {
                    this.lblArea1PositionInfo6.Text = positionDesc;
                    this.lblArea1PositionInfo6.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo7.Text))
                {
                    this.lblArea1PositionInfo7.Text = positionDesc;
                    this.lblArea1PositionInfo7.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo8.Text))
                {
                    this.lblArea1PositionInfo8.Text = positionDesc;
                    this.lblArea1PositionInfo8.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo9.Text))
                {
                    this.lblArea1PositionInfo9.Text = positionDesc;
                    this.lblArea1PositionInfo9.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo10.Text))
                {
                    this.lblArea1PositionInfo10.Text = positionDesc;
                    this.lblArea1PositionInfo10.ForeColor = System.Drawing.Color.Yellow;
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo1.Text))
                {
                    this.lblArea2PositionInfo1.Text = positionDesc;
                    this.lblArea2PositionInfo1.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo2.Text))
                {
                    this.lblArea2PositionInfo2.Text = positionDesc;
                    this.lblArea2PositionInfo2.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo3.Text))
                {
                    this.lblArea2PositionInfo3.Text = positionDesc;
                    this.lblArea2PositionInfo3.ForeColor = System.Drawing.Color.Yellow;
                }
            }

            this.Refresh();
            this.Update();

            ////this.PlayMusic(lineCode, PositionType.Help);
        }

        /// <summary>
        /// Update Alarm Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateAlarmPositionInfo(string positionDesc, int positionCount)
        {
            this.hasSHAPositionLine1 = true;
            this.hasSAPositionLine1 = true;

            if (positionCount > 3)
            {
                if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo1.Text))
                {
                    this.lblArea1PositionInfo1.Text = positionDesc;
                    this.lblArea1PositionInfo1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo2.Text))
                {
                    this.lblArea1PositionInfo2.Text = positionDesc;
                    this.lblArea1PositionInfo2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo3.Text))
                {
                    this.lblArea1PositionInfo3.Text = positionDesc;
                    this.lblArea1PositionInfo3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo4.Text))
                {
                    this.lblArea1PositionInfo4.Text = positionDesc;
                    this.lblArea1PositionInfo4.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo5.Text))
                {
                    this.lblArea1PositionInfo5.Text = positionDesc;
                    this.lblArea1PositionInfo5.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo6.Text))
                {
                    this.lblArea1PositionInfo6.Text = positionDesc;
                    this.lblArea1PositionInfo6.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo7.Text))
                {
                    this.lblArea1PositionInfo7.Text = positionDesc;
                    this.lblArea1PositionInfo7.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo8.Text))
                {
                    this.lblArea1PositionInfo8.Text = positionDesc;
                    this.lblArea1PositionInfo8.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo9.Text))
                {
                    this.lblArea1PositionInfo9.Text = positionDesc;
                    this.lblArea1PositionInfo9.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo10.Text))
                {
                    this.lblArea1PositionInfo10.Text = positionDesc;
                    this.lblArea1PositionInfo10.ForeColor = System.Drawing.Color.Red;
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo1.Text))
                {
                    this.lblArea2PositionInfo1.Text = positionDesc;
                    this.lblArea2PositionInfo1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo2.Text))
                {
                    this.lblArea2PositionInfo2.Text = positionDesc;
                    this.lblArea2PositionInfo2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo3.Text))
                {
                    this.lblArea2PositionInfo3.Text = positionDesc;
                    this.lblArea2PositionInfo3.ForeColor = System.Drawing.Color.Red;
                }
            }

            this.Refresh();
            this.Update();

            ////this.PlayMusic(lineCode, PositionType.Alarm);
        }

        /// <summary>
        /// Update Stop Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateStopPositionInfo(string positionDesc, int positionCount)
        {
            this.hasSHAPositionLine1 = true;
            this.hasSAPositionLine1 = true;

            if (positionCount > 3)
            {
                if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo1.Text))
                {
                    this.lblArea1PositionInfo1.Text = positionDesc;
                    this.lblArea1PositionInfo1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo2.Text))
                {
                    this.lblArea1PositionInfo2.Text = positionDesc;
                    this.lblArea1PositionInfo2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo3.Text))
                {
                    this.lblArea1PositionInfo3.Text = positionDesc;
                    this.lblArea1PositionInfo3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo4.Text))
                {
                    this.lblArea1PositionInfo4.Text = positionDesc;
                    this.lblArea1PositionInfo4.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo5.Text))
                {
                    this.lblArea1PositionInfo5.Text = positionDesc;
                    this.lblArea1PositionInfo5.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo6.Text))
                {
                    this.lblArea1PositionInfo6.Text = positionDesc;
                    this.lblArea1PositionInfo6.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo7.Text))
                {
                    this.lblArea1PositionInfo7.Text = positionDesc;
                    this.lblArea1PositionInfo7.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo8.Text))
                {
                    this.lblArea1PositionInfo8.Text = positionDesc;
                    this.lblArea1PositionInfo8.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo9.Text))
                {
                    this.lblArea1PositionInfo9.Text = positionDesc;
                    this.lblArea1PositionInfo9.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea1PositionInfo10.Text))
                {
                    this.lblArea1PositionInfo10.Text = positionDesc;
                    this.lblArea1PositionInfo10.ForeColor = System.Drawing.Color.Red;
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo1.Text))
                {
                    this.lblArea2PositionInfo1.Text = positionDesc;
                    this.lblArea2PositionInfo1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo2.Text))
                {
                    this.lblArea2PositionInfo2.Text = positionDesc;
                    this.lblArea2PositionInfo2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblArea2PositionInfo3.Text))
                {
                    this.lblArea2PositionInfo3.Text = positionDesc;
                    this.lblArea2PositionInfo3.ForeColor = System.Drawing.Color.Red;
                }
            }

            this.Refresh();
            this.Update();

            ////this.PlayMusic(lineCode, PositionType.Stop);
        }

        /// <summary>
        /// Get Unnormal Position Num
        /// </summary>
        /// <param name="positionStatus"></param>
        /// <returns></returns>
        public int GetUnnormalPositionNum(Dictionary<int, string> positionStatus, string lineCode)
        {
            int positionCount = 0;

            foreach (int position in positionStatus.Keys)
            {
                DataRow row = this.GetPositionDataRow(position, lineCode);

                if (row == null)
                {
                    continue;
                }

                if (!string.Equals("0000", positionStatus[position].ToString().Trim()))
                {
                    positionCount++;
                }
            }

            return positionCount;
        }

        /// <summary>
        /// Update Position Info
        /// </summary>
        /// <param name="positionStatus"></param>
        /// <param name="lineCode"></param>
        public void UpdatePositionInfo(Dictionary<int, string> positionStatus, string lineCode)
        {
            this.ClearPositionInfo();

            int positionCount = this.GetUnnormalPositionNum(positionStatus, lineCode);

            this.SetOperationArea(positionCount);

            foreach (int position in positionStatus.Keys)
            {
                DataRow row = this.GetPositionDataRow(position, lineCode);
                PositionType positionType = this.GetPositionType(row);

                string positionDesc = this.GetPositionDesc(row);
                string sPositionID = this.GetSPositionID(row);
                string fPositionID = this.GetFPositionID(row);
                PositionStatus pStatus = PositionStatus.Normal;

                if (!string.Equals("0000", positionStatus[position].ToString().Trim()))
                {
                    pStatus = PositionStatus.Unnormal;

                    if (!string.IsNullOrWhiteSpace(positionDesc))
                    {
                        if ((positionType == PositionType.Help)
                            || (positionType == PositionType.Unknown))
                        {
                            this.UpdateHelpPositionInfo(positionDesc, positionCount);
                        }
                        else if (positionType == PositionType.Alarm)
                        {
                            this.UpdateAlarmPositionInfo(positionDesc, positionCount);
                        }
                        else if (positionType == PositionType.Stop)
                        {
                            this.UpdateStopPositionInfo(positionDesc, positionCount);
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    pStatus = PositionStatus.Normal;
                }

                this.UpdatePositionLog(position, lineCode, positionType, sPositionID, fPositionID, pStatus);
            }

            try
            {
                this.StopMusic();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Stop Music
        /// </summary>
        private void StopMusic()
        {
            if (!this.hasSAPositionLine1)
            {
                this.mediaPlayerAlarmLine1.Ctlcontrols.stop();
                this.mediaPlayerStopLine1.Ctlcontrols.stop();
            }

            if (!this.hasHPositionLine1)
            {
                this.mediaPlayerHelpLine1.Ctlcontrols.stop();
            }
        }

        /// <summary>
        /// Update Position Log
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lineCode"></param>
        /// <param name="positionType"></param>
        /// <param name="sPositionID"></param>
        /// <param name="fPositionID"></param>
        /// <param name="positionStatus"></param>
        private void UpdatePositionLog(int position, string lineCode, PositionType positionType, string sPositionID, string fPositionID, PositionStatus positionStatus)
        {
            string logDate = DateTime.Now.ToString("yyyy-MM-dd");
            string logTime = DateTime.Now.ToString("HH:mm:ss");
            string shift = string.Empty;
            string type = string.Empty;
            string stopTime = string.Empty;
            string helpTime = string.Empty;
            string alarmTime = string.Empty;
            TimeSpan span = new TimeSpan(0);
            bool isNeedUpdatePositionLog = false;

            if (positionStatus == PositionStatus.Unnormal)
            {
                if (this.positionStatusInfoLine1.ContainsKey(position))
                {
                    if (this.positionStatusInfoLine1[position].Status != positionStatus)
                    {
                        this.positionStatusInfoLine1[position].Status = positionStatus;
                        this.positionStatusInfoLine1[position].StatusBeginTime = DateTime.Now;
                    }
                }
                else
                {
                    PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                    positionStatusInfo.Status = positionStatus;
                    positionStatusInfo.StatusBeginTime = DateTime.Now;

                    this.positionStatusInfoLine1.Add(position, positionStatusInfo);
                }
            }
            else if (positionStatus == PositionStatus.Normal)
            {
                if (this.positionStatusInfoLine1.ContainsKey(position))
                {
                    if (this.positionStatusInfoLine1[position].Status != positionStatus)
                    {
                        this.positionStatusInfoLine1[position].Status = positionStatus;
                        this.positionStatusInfoLine1[position].StatusEndTime = DateTime.Now;

                        span = this.positionStatusInfoLine1[position].StatusEndTime
                             - this.positionStatusInfoLine1[position].StatusBeginTime;

                        shift = this.GetShift(this.shiftName);

                        isNeedUpdatePositionLog = true;
                    }
                }
            }

            if (isNeedUpdatePositionLog)
            {
                if ((positionType == PositionType.Alarm)
                    || (positionType == PositionType.Unknown))
                {
                    type = "A";
                    alarmTime = span.TotalSeconds.ToString("0");
                }
                else if (positionType == PositionType.Help)
                {
                    type = "H";
                    helpTime = span.TotalSeconds.ToString("0");
                }
                else if (positionType == PositionType.Stop)
                {
                    type = "S";
                    stopTime = span.TotalSeconds.ToString("0");
                }
                else
                {
                    return;
                }

                Position.UpdatePositionLogByKey(lineCode,
                                                logDate,
                                                shift,
                                                logTime,
                                                type,
                                                sPositionID,
                                                fPositionID,
                                                stopTime,
                                                helpTime,
                                                alarmTime);

                Position.UpdatePositionLogTempByKey(lineCode,
                                                    logDate,
                                                    shift,
                                                    logTime,
                                                    type,
                                                    sPositionID,
                                                    fPositionID,
                                                    stopTime,
                                                    helpTime,
                                                    alarmTime);
            }
        }

        /// <summary>
        /// Play Music
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="positionType"></param>
        private void PlayMusic(string lineCode, PositionType positionType)
        {
            if (this.axWindowsMediaPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                return;
            }

            string musicName = string.Empty;

            if ((positionType == PositionType.Help)
                || (positionType == PositionType.Unknown))
            {
                musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys1);

                this.axWindowsMediaPlayer = this.mediaPlayerHelpLine1;
            }
            else if (positionType == PositionType.Alarm)
            {
                musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys1);

                this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine1;
            }
            else if (positionType == PositionType.Stop)
            {
                musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys1);

                this.axWindowsMediaPlayer = this.mediaPlayerStopLine1;
            }

            if (!string.IsNullOrWhiteSpace(musicName))
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory
                                + ConfigurationManager.AppSettings["UploadFileDirectory"]
                                + "/" + lineCode + "/"
                                + musicName;

                FileInfo file = new FileInfo(filePath);
                if (file.Exists)
                {
                    try
                    {
                        this.axWindowsMediaPlayer.URL = file.FullName;
                        this.axWindowsMediaPlayer.Ctlcontrols.play();
                    }
                    catch
                    {
                    }
                }
            }
        }

        /// <summary>
        /// Has SHA Position
        /// </summary>
        /// <returns></returns>
        public bool HasSHAPosition()
        {
            if (this.hasSHAPositionLine1)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Change Shift Name
        /// </summary>
        /// <returns></returns>
        private bool ChangeShiftName()
        {
            bool isShiftNameChanged = false;

            string shiftInfoLine1 = string.Empty;

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                shiftInfoLine1 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys1);

                if (!string.Equals(this.shiftName.Trim(), shiftInfoLine1.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (isShiftNameChanged)
            {
                this.shiftName = shiftInfoLine1.Trim();
            }

            return isShiftNameChanged;
        }

        /// <summary>
        /// Get Shift
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetShift(string text)
        {
            if (text.Contains("1"))
            {
                return "1";
            }
            else if (text.Contains("2"))
            {
                return "2";
            }
            else if (text.Contains("3"))
            {
                return "3";
            }

            return "0";
        }

        /// <summary>
        /// Play Music By Priority
        /// </summary>
        private void PlayMusicByPriority()
        {
            if (this.hasSAPositionLine1)
            {
                this.PlayMusic(ConfigInfo.LineCode1, PositionType.Alarm);
            }
            else if (this.hasHPositionLine1)
            {
                this.PlayMusic(ConfigInfo.LineCode1, PositionType.Help);
            }
        }

        /// <summary>
        /// Stop All Music
        /// </summary>
        private void StopAllMusic()
        {
            try
            {
                this.axWindowsMediaPlayer.Ctlcontrols.stop();
                this.mediaPlayerAlarmLine1.Ctlcontrols.stop();
                this.mediaPlayerHelpLine1.Ctlcontrols.stop();
                this.mediaPlayerStopLine1.Ctlcontrols.stop();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Quit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.StopAllMusic();
            this.Close();
            this.isClosedApp = true;
            //Application.Exit();
        }

        /// <summary>
        /// timer Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.ChangeShiftName();
        }

        /// <summary>
        /// timerMusic Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerMusic_Tick(object sender, EventArgs e)
        {
            this.PlayMusicByPriority();
        }

        private void btnSeBeiGuZhang_Click(object sender, EventArgs e)
        {
            SeBeiGuZhangView form = new SeBeiGuZhangView();
            form.ShowDialog();
        }

        private void btnPinZhiBuLiang_Click(object sender, EventArgs e)
        {
            PinZhiBuLiangView form = new PinZhiBuLiangView();
            form.ShowDialog();
        }

        private void btnBuPinQianPin_Click(object sender, EventArgs e)
        {
            BuPinQianPinView form = new BuPinQianPinView();
            form.ShowDialog();
        }

        private void timerProductInfo_Tick(object sender, EventArgs e)
        {
            this.lblActualCompletedLine1.Text = ConfigInfo.ActualCompleted;
            this.lblStopTimeLine1.Text = ConfigInfo.StopTime;
        }

        private void PositionManagementView_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                ConfigInfo.SaveNowPlanValue();
            }
            catch
            {

            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            Setup form = new Setup();
            form.ShowDialog();
        }
    }
}
